package purchaseStore

import (
	"context"
	"github.com/google/uuid"
	"insighttracking/apps/purchase/internal/svc"
	"insighttracking/apps/purchase/internal/types"
	"insighttracking/common/utils"
	"insighttracking/query/service"
	"net/http"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetPurchaseBatchNumberApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetPurchaseBatchNumberApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetPurchaseBatchNumberApiLogic {
	hostname, _ := os.Hostname()
	return &GetPurchaseBatchNumberApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetPurchaseBatchNumberApiLogic) GetPurchaseBatchNumberListApi(req *types.GetBatchNumberReq) (resp *types.GetBatchNumberResp, err error) {
	resp = &types.GetBatchNumberResp{}
	results, err := service.SpuShelfGridInventoryService.GetAvailableBatchNumberApi(l.ctx, req.ShelfID, req.SpuID)
	if err != nil {
		return
	}
	list := make([]*types.BatchNumberList, 0)
	for _, v := range results {
		list = append(list, &types.BatchNumberList{
			SpuName:              v.SpuName,
			ShelfCode:            v.ShelfCode,
			InventoryBatchNumber: v.BatchNumber,
			CreatedAt:            utils.TimestampToString(v.CreatedAt),
			Supplier:             v.SupplierName,
			InventoryCount:       v.InventoryCount,
			ExpirationDate:       utils.TimestampToString(v.ExpirationDate),
		})
	}
	resp.List = list
	return
}
